package com.xiaoy.core.tag.html;

import java.lang.reflect.Method;
import java.util.List;

import org.springside.modules.orm.Page;

import com.xiaoy.core.tag.GridDataTag;

public class GridDataHtml {
	
//	List<ColumnTag>columnTags = null;
	
	String table = "<table>$tr<table>";
	
	String dataTable = "<table>$header$body$bar</table>";
	String tr = "<tr>$td</tr>";
	String td = "<td>$value</td>";
	String th = "<th>$value</th>";
	
	private GridDataTag gridDataTag = null;
	
	public GridDataHtml () {}
	
	public GridDataHtml (GridDataTag gridDataTag) {
		this.gridDataTag=gridDataTag;
	}
	
	public String initHeader () {
		
		List<GridColumnHtml>columns = gridDataTag.getcolumnHtmls();
		StringBuffer sb= new StringBuffer();
		
		for (GridColumnHtml column : columns) {
			String title = column.getTitle();
			sb.append(th.replace("$value", title));
		}
		return tr.replace("$td", sb);
	}
	public String initBody () {
		
		List<GridColumnHtml>columns = gridDataTag.getcolumnHtmls();
		List dataList = gridDataTag.getPage().getResult();
		if (dataList == null) {
			dataList = gridDataTag.getDataList();
		}
		
		StringBuffer sb = null;
		StringBuffer body = new StringBuffer();
		for (Object obj :dataList) {
			sb = new StringBuffer();
			for (GridColumnHtml column : columns) {
				String name = column.getName();
				name = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
				try {
					Method method = obj.getClass().getMethod(name);
					Object o = method.invoke(obj);
					if (o!=null) {
						sb.append(td.replace("$value", o.toString()));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			body.append(tr.replace("$td", sb));
		}
		return body.toString();
	}
	public String initBar () {
		Page page = gridDataTag.getPage();
		String url = gridDataTag.getUrl();
		if(page.getTotalPages()==0) {
			page.setTotalCount(1);
			page.setPageNo(1);
		}
		String str = "";
		str+="页次:"+page.getPageNo()+"/"+page.getTotalPages()+"页&nbsp;&nbsp;";
		str+="共"+page.getTotalCount()+"条记录&nbsp;&nbsp;";
		str += "<a href='"+url+"&pageBean.pageNo="+page.getFirst()+"&pageBean.pageSize="+page.getPageSize()+"'>首页</a>&nbsp;";
		str +=  "<a href='"+url+"&pageBean.pageNo="+page.getPrePage()+"&pageBean.pageSize="+page.getPageSize()+"'>上一页</a>&nbsp;";
		str +=  "<a href='"+url+"&pageBean.pageNo="+page.getNextPage()+"&pageBean.pageSize="+page.getPageSize()+"'>下一页</a>&nbsp;";
		str +=  "<a href='"+url+"&pageBean.pageNo="+page.getTotalPages()+"&pageBean.pageSize="+page.getPageSize()+"'>尾页</a>&nbsp;";
		return str;
	}
	
//	public String initPage () {
//		initList(page.getResult());
//	}
	public String initList (List dataList) {
		String sb = "";
		
	
		
//		List dataList = gridDataTag.getDataList();
		List<GridColumnHtml>columns = gridDataTag.getcolumnHtmls();
		sb="";
		String th = "";
		
		for (GridColumnHtml column : columns) {
			String title = column.getTitle();
			th += td.replace("$value", title);
		}
		
		String header = tr.replace("$td", th);
		
		String body = "";
		if(dataList == null) {
			return "";
		}
		for (Object obj :dataList) {
			th = "";
			for (GridColumnHtml column : columns) {
				String name = column.getName();
				
				name = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
				
				try {
					Method method = obj.getClass().getMethod(name);
					Object o = method.invoke(obj);
					if (o!=null) {
						th += td.replace("$value", o.toString());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			body += tr.replace("$td", th);
		}
		
		return table.replace("$tr", header+body);
	}
	
	public String init () {
		return dataTable.replace("$header", initHeader()).replace("$body", initBody()).replace("$bar", initBar());
	}
	
	
	
}
